#include "dialogs/DialogFileSelect.h"

#include "scenes/SceneMainMenu.h"

DialogFileSelect::DialogFileSelect( GameApplication& theApp, gdn::Scene& theScene )
   : App( theApp ),
     Scene( theScene )
{
	// None
}

void DialogFileSelect::Initialize()
{
	background.SetImage( *App.Images.GetResource( App.GameResources[ "gui dialog background" ] ) );

	gdn::Image* newloadImage = App.Images.GetResource( App.GameResources[ "gui button long" ] );
	gdn::Font * font         = App.Fonts.GetResource( App.GameResources[ "font sansation" ] );

	gdn::Image* scrollerImageUp     = App.Images.GetResource( App.GameResources[ "gui button up" ] );
	gdn::Image* scrollerImageDown   = App.Images.GetResource( App.GameResources[ "gui button down" ] );
	gdn::Image* scrollerScrollField = App.Images.GetResource( App.GameResources[ "gui scroller field" ] );
	gdn::Image* scrollerScrollBar   = App.Images.GetResource( App.GameResources[ "gui scroller bar" ] );
	gdn::Image* scrollerBackground  = App.Images.GetResource( App.GameResources[ "gui scroller background" ] );
	gdn::Image* scrollerHighlight   = App.Images.GetResource( App.GameResources[ "gui scroller highlight" ] );

	gdn::Image* closeImage = App.Images.GetResource( App.GameResources[ "gui button small" ] );

	//std::cout << newloadImage << font << scrollerImageUp << scrollerImageDown << scrollerScrollField << scrollerScrollBar << scrollerBackground << scrollerHighlight << std::endl;

	newButton = gui::Button( gdn::Vector2f( 10 + ( 122 * 0 ) + ( 5 * 0 ), 160 ),
							 "New", "New", "New",
							 gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
							 *newloadImage, *font, 19,
							 gdn::Rectanglei( 0, 0, 122, 32 ), gdn::Rectanglei( 122, 0, 122, 32 ), gdn::Rectanglei( 244, 0, 122, 32 ) );

	loadButton = gui::Button( gdn::Vector2f( 10 + ( 122 * 1 ) + ( 5 * 1 ), 160 ),
							  "Load", "Load", "Load",
							  gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				              *newloadImage, *font, 19,
				              gdn::Rectanglei( 0, 0, 122, 32 ), gdn::Rectanglei( 122, 0, 122, 32 ), gdn::Rectanglei( 244, 0, 122, 32 ) );

	fileSelector = gui::Scroller( gdn::Vector2f( 10, 10 ), gdn::Vector2f( 268 - 20, 200 - ( 10 * 2 ) - 32 - 10 ), App.GameFiles.GetFileList(), 0,
	                              *scrollerImageUp, *scrollerImageDown, *scrollerScrollField, *scrollerScrollBar, *scrollerBackground, *scrollerHighlight,
	                              *font, 13,

	                              gdn::Rectanglei( 0, 0, 24, 24 ), gdn::Rectanglei( 24, 0, 24, 24 ), gdn::Rectanglei( 48, 0, 24, 24 ),

	                              gdn::Rectanglei( 0, 0, 24, 24 ), gdn::Rectanglei( 24, 0, 24, 24 ), gdn::Rectanglei( 48, 0, 24, 24 ),

	                              gdn::Rectanglei( 0, 0, 24, 2 ), gdn::Rectanglei( 24, 0, 24, 2 ), gdn::Rectanglei( 48, 0, 24, 2 ),
	                              gdn::Rectanglei( 0, 2, 24, 1 ), gdn::Rectanglei( 24, 2, 24, 1 ), gdn::Rectanglei( 48, 2, 24, 1 ),
	                              gdn::Rectanglei( 0, 3, 24, 2 ), gdn::Rectanglei( 24, 3, 24, 2 ), gdn::Rectanglei( 48, 3, 24, 2 ),

	                              gdn::Rectanglei( 0, 0, 3, 3 ),  gdn::Rectanglei( 3, 0, 1, 3 ),   gdn::Rectanglei( 4, 0, 3, 3 ),
	                              gdn::Rectanglei( 0, 3, 3, 1 ),  gdn::Rectanglei( 3, 3, 1, 1 ),   gdn::Rectanglei( 4, 3, 3, 1 ),
	                              gdn::Rectanglei( 0, 4, 3, 3 ),  gdn::Rectanglei( 3, 4, 1, 3 ),   gdn::Rectanglei( 4, 4, 3, 3 ) );

	closeButton = gui::Button( gdn::Vector2f( 244, 0 ),
							   "X", "X", "X",
							   gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				               *closeImage, *font, 15,
				               gdn::Rectanglei( 0, 0, 24, 24 ), gdn::Rectanglei( 24, 0, 24, 24 ), gdn::Rectanglei( 48, 0, 24, 24 ) );
}

void DialogFileSelect::Terminate()
{
	// None
}

void DialogFileSelect::Step( gdn::Event& Event )
{
	if ( newButton.Process( Event ) == GUI_BUTTON_RELEASE )
	{
		App.SetActiveScene( "New File" );
	}

	fileSelector.Process( Event );

	if ( loadButton.Process( Event ) == GUI_BUTTON_RELEASE )
	{
		App.CurrentFile = GameFile( fileSelector.GetSelectedItem() );
		App.SetActiveScene( "Game" );
	}

	if ( closeButton.Process( Event ) == GUI_BUTTON_RELEASE )
	{
		static_cast< SceneMainMenu& >( Scene ).CloseDialog();
	}
}

void DialogFileSelect::Draw( gdn::RenderImage& renderImage )
{
	renderImage.Clear();

	renderImage.Draw( background );

	newButton.Draw( renderImage );
	loadButton.Draw( renderImage );

	fileSelector.Draw( renderImage );

	closeButton.Draw( renderImage );

	renderImage.Display();
}
